package com.example.sls.dao.logistics;

import com.example.sls.pojo.logistics.AnnouncementHistory;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 公告历史记录表(AnnouncementHistory)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-24 08:24:23
 */
public interface AnnouncementHistoryDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from announcement_history where id=#{id}")
  AnnouncementHistory queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param announcementHistory 查询条件
   * @param pageSize            分页对象
   * @param offset              偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from announcement_history where 1=1" +
          " and (id=#{announcementHistory.id} or (isnull(#{announcementHistory.id}) or #{announcementHistory.id}=0))" +
          " and (announcement_id=#{announcementHistory.announcementId} or (isnull(#{announcementHistory.announcementId}) or #{announcementHistory.announcementId}=0))" +
          " and (modified_at=#{announcementHistory.modifiedAt} or (isnull(#{announcementHistory.modifiedAt}) or #{announcementHistory.modifiedAt}=''))" +
          " and (modified_by=#{announcementHistory.modifiedBy} or (isnull(#{announcementHistory.modifiedBy}) or #{announcementHistory.modifiedBy}=0))" +
          " and (changes=#{announcementHistory.changes} or (isnull(#{announcementHistory.changes}) or #{announcementHistory.changes}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<AnnouncementHistory> queryAllByLimit(AnnouncementHistory announcementHistory, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from announcement_history")
  long count();

  /**
   * 新增数据
   *
   * @param announcementHistory 实例对象
   * @return 影响行数
   */
  @Insert("insert into announcement_history (id,announcement_id,modified_at,modified_by,changes) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(!isnull(#{announcementId}),#{announcementId},null)," +
          "if(!isnull(#{modifiedAt}),#{modifiedAt},null)," +
          "if(!isnull(#{modifiedBy}),#{modifiedBy},null)," +
          "if(!isnull(#{changes}),#{changes},null)" +
          ")")
  int insert(AnnouncementHistory announcementHistory);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param announcementHistory 实例对象
   * @return 影响行数
   */
  @Update("update announcement_history set " +
          "announcement_id=if(isnull(#{announcementId}),announcement_id,#{announcementId})," +
          "modified_at=if(isnull(#{modifiedAt}),modified_at,#{modifiedAt})," +
          "modified_by=if(isnull(#{modifiedBy}),modified_by,#{modifiedBy})," +
          "changes=if(isnull(#{changes}),changes,#{changes})" +
          " where id=#{id}")
  int update(AnnouncementHistory announcementHistory);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from announcement_history where id=#{id}")
  int deleteById(Integer id);
}

